$(function(){
	console.log(111)
	var page = 1;
	var staffall;
	var limitlist;
	var tddata=['limit_name','limittype','need_check','state_main'];
//							
				
	var addoperation=[`<td>
			<div class="tpl-table-black-operation">
				<a class="detail" href="javascript:;">
					<i class="am-icon-pencil"></i> 详情
				</a>
				<a class="change" href="javascript:;">
					<i class="am-icon-pencil"></i> 编辑
				</a>
				<a href="javascript:;" class="tpl-table-black-operation-del forbidden" data-method="forbidden">
					<i class="am-icon-trash"></i> 禁用
				</a>
				<a href="javascript:;" class="tpl-table-black-operation-del delete-list" data-method="delete">
					<i class="am-icon-trash"></i> 删除
				</a>
			</div>
		</td>`,`<td>
			<div class="tpl-table-black-operation">
				<a class="detail" href="javascript:;">
					<i class="am-icon-pencil"></i> 详情
				</a>
				<a class="change" href="javascript:;">
					<i class="am-icon-pencil"></i> 编辑
				</a>
				<a href="javascript:;" class="startusing" data-method="startusing">
					<i class="am-icon-pencil"></i> 启用
				</a>
				<a href="javascript:;" class="tpl-table-black-operation-del delete-list" data-method="delete">
					<i class="am-icon-trash"></i> 删除
				</a>
			</div>
		</td>`]
	$.ajax({
		type: "post",
		url: "{:url('/unit/staff/all')}",
		data: {},
		success: function(res) {
			staffall=res.data;
		}
	})
	postlimitall()
	function postlimitall(Load){
	
	$.ajax({
		type: "post",
		url: "{:url('/unit/limit/all')}",
		success: function(res) {
			if(Load){
				layer.close(Load);
			}
			
			$("#loading-first").fadeOut(500);
			limitlist=res.data;
			for(var i=0;i<limitlist.length;i++){
				if(limitlist[i].state==1){
					limitlist[i].state_main='启用中';
				}else{
					limitlist[i].state_main='禁用中';
				}
				limitlist[i].limit_rule=JSON.parse(limitlist[i].limit_rule)
				if(limitlist[i].limit_type==1){
					limitlist[i].limittype='财政';
					limitlist[i].need_check='需审批';
				}else{
					if(limitlist[i].limit_rule.need_check){
						limitlist[i].need_check='需审批';
					}else{
						limitlist[i].need_check='不需审批';
					}
					limitlist[i].limittype='单位';
				}
				
			}
			console.log(limitlist);
			ajaxlist(1);
			
			
			function ajaxlist(page){
				var pagemain=pagechang(limitlist.length, 10, page);
				var tbodymain = tbodymainf1(limitlist, page, tddata, addoperation, 'limit_id');
				$('.pagination').html(pagemain)
				$('tbody').html(tbodymain)
				$('.laquo').on('click',function(){
					ajaxlist(page-1)
				})
				$('.pageli').on('click',function(){
					ajaxlist($(this).html())
				});
				$('.raquo').on('click',function(){
					ajaxlist(page+1)
				});
				$('.change').on('click', function(){
					window.location.href = '{:url("edit")}?limit_id=' + $(this).closest('tr').data('id')
				});
				$('.startusing').on('click', function() {
					popup(this, "{:url('state')}");
			
				});
				//禁用
				$('.forbidden').on('click', function() {
					popup(this, "{:url('state')}");
			
				});
				$('.delete-list').on('click', function() {
                    var that=this 
                    deletelist($(this),"{:url('delete')}",postlimitall)
				});
				$('.detail').on('click',function(){
					var detail_main;
					console.log($(this).closest('tr').data('id'))
					console.log(limitlist)
					
					for(var deidx=0;deidx<limitlist.length;deidx++){
						if($(this).closest('tr').data('id')==limitlist[deidx].limit_id){
							detail_main=limitlist[deidx]
						}
					}
					console.log(detail_main)
					var use_date='',address='',quota_day='',use_time='',approve_flowpath='',limittype='',type='',car_type='';
					if(detail_main.limit_type==1){
						limittype='财政'
					}else{
						limittype='单位'
					}
					if(detail_main.limit_rule){
						if(detail_main.limit_rule.address){
							address=detail_main.limit_rule.address;
						}else{
							address='暂无用车地点'
						}
						//审批员
						if(detail_main.limit_rule.checker){
							approve_flowpath+=`<div class='order_detail_h2'>审批流程</div>`
							if(detail_main.limit_type==1){
								approvedetail()
							}else{
								if(detail_main.limit_rule.need_check){
									if(detail_main.limit_rule.need_check=='on'){
									approvedetail()
									}else{
										approve_flowpath+='<div class="am-u-sm-12 cl-p order_detail_text"><div class="detail-list">不需审批</div></div>'
									}
								}else{
									approve_flowpath+='<div class="am-u-sm-12 cl-p order_detail_text"><div class="detail-list">不需审批</div></div>'
								}
							}
							
							function approvedetail(){
							if(detail_main.limit_rule.checker[1]){
								var one_approve='';
								console.log(detail_main.limit_rule.checker[1])
								for(var check1=0;check1<detail_main.limit_rule.checker[1].length;check1++){
									for(var check2=0;check2<staffall.length;check2++){
										if(detail_main.limit_rule.checker[1][check1]==staffall[check2].passenger_id){
											console.log(detail_main.limit_rule.checker[1][check1])
											one_approve+=(staffall[check2].name+' ')
											
										}
									}
								}
								approve_flowpath+=`<div class="am-u-sm-12 cl-p order_detail_text">
									<div class="detail-list">第一级审批人:</div>${one_approve}
								</div>`
								console.log(one_approve)
							}else{
								one_approve='无审批员'
							}
							if(detail_main.limit_rule.checker[2]){
								var two_approve='';
								console.log(detail_main.limit_rule.checker[2])
								for(var check1=0;check1<detail_main.limit_rule.checker[2].length;check1++){
									for(var check2=0;check2<staffall.length;check2++){
										if(detail_main.limit_rule.checker[2][check1]==staffall[check2].passenger_id){
											console.log(detail_main.limit_rule.checker[2][check1])
											two_approve+=staffall[check2].name+' ';
											
										}
									}
								}
								approve_flowpath+=`<div class="am-u-sm-12 cl-p order_detail_text">
													<div class="detail-list">第二级审批人:</div>${two_approve}
												</div>`
							}else{
								two_approve='无审批员'
							}
							if(detail_main.limit_rule.checker[3]){
								var three_approve='';
								console.log(detail_main.limit_rule.checker[3])
								for(var check1=0;check1<detail_main.limit_rule.checker[3].length;check1++){
									for(var check2=0;check2<staffall.length;check2++){
										if(detail_main.limit_rule.checker[3][check1]==staffall[check2].passenger_id){
											console.log(detail_main.limit_rule.checker[3][check1])
											three_approve+=staffall[check2].name+' ';
											
										}
									}
								}
								approve_flowpath+=`<div class="am-u-sm-12 cl-p order_detail_text">
													<div class="detail-list">第三级审批人:</div>${three_approve}
												</div>`
							}else{
								three_approve='无审批员'
							}
							}
						}else{
							approve_flowpath+=`<div class='order_detail_h2'>审批流程</div>`
							approve_flowpath+='<div class="am-u-sm-12 cl-p order_detail_text"><div class="detail-list">无审批员</div></div>'
						}
						//每日限额
						if(detail_main.limit_rule.quota_day){
								quota_day=detail_main.limit_rule.quota_day
						}else{
							quota_day='不限额'
						}
						//用车日期
						if(detail_main.limit_rule.date){
							use_date=detail_main.limit_rule.date.start+'至'+detail_main.limit_rule.date.end
						}else{
							use_date='不限'
						}
						//车型
						if(detail_main.limit_rule.type){
							for(var typeidx=0;typeidx<detail_main.limit_rule.type.length;typeidx++){
								if(detail_main.limit_rule.type[typeidx]==2){
									type+=" 公务车 "
								}else if(detail_main.limit_rule.type[typeidx]==3){
									type+=" 公包车 "
								}else if(detail_main.limit_rule.type[typeidx]==4){
									type+=" 约租车 "
								}else if(detail_main.limit_rule.type[typeidx]==5){
									type+=" 私包车 "
								}
							}
						}
						if(detail_main.limit_rule.car_type){
							for(var cartypeidx=0;cartypeidx<detail_main.limit_rule.car_type.length;cartypeidx++){
								if(detail_main.limit_rule.car_type[cartypeidx]==2){
									car_type+=" 优选型 "
								}else if(detail_main.limit_rule.car_type[cartypeidx]==3){
									car_type+=" 舒适型 "
								}else if(detail_main.limit_rule.car_type[cartypeidx]==4){
									car_type+=" 豪华型 "
								}else if(detail_main.limit_rule.car_type[cartypeidx]==5){
									car_type+=" 商务型 "
								}else if(detail_main.limit_rule.car_type[cartypeidx]==6){
									car_type+=" 大巴  "
								}else if(detail_main.limit_rule.car_type[cartypeidx]==7){
									car_type+=" 小巴 "
								}else if(detail_main.limit_rule.car_type[cartypeidx]==8){
									car_type+=" 中巴 "
								}else if(detail_main.limit_rule.car_type[cartypeidx]==9){
									car_type+=" 执法车 "
								}
							}
						}
						//用车时间
						if(detail_main.limit_rule.time){
							use_time+=`<div class='am-fl'>`
							if(detail_main.limit_rule.time.work&&detail_main.limit_rule.time.work.start){
								use_time+='工作日'+detail_main.limit_rule.time.work.start+'至'+detail_main.limit_rule.time.work.end
							}else if(detail_main.limit_rule.time.rest&&detail_main.limit_rule.time.rest.start){
								use_time+='节假日'+detail_main.limit_rule.time.rest.start+'至'+detail_main.limit_rule.time.rest.end
							}else if(detail_main.limit_rule.time.week){
								for(var key  in detail_main.limit_rule.time.week){ 
									if(key==7){
										use_time+=`<div>周日 `
									}else if(key==1){
										use_time+=`<div>周一`
									}else if(key==2){
										use_time+=`<div>周二`
									}else if(key==3){
										use_time+=`<div>周三`
									}else if(key==4){
										use_time+=`<div>周四`
									}else if(key==5){
										use_time+=`<div>周五`
									}else if(key==6){
										use_time+=`<div>周六`
									}
								use_time+=` ${detail_main.limit_rule.time.week[key].start}-${detail_main.limit_rule.time.week[key].end}</div>`
								
								}
							}else{
								use_time+='不限';
							}
							use_time+=`</div>`;
						}else{
							use_time='不限';
						}
					}else{
						detail_main.limit_rule={};
						use_date='不限';
						use_time='不限';
					}
					
					layer.open({
						title: false,
						type: 1,
						skin: 'layui-layer-rim', //加上边框
						area: ['420px', '500px'], 
						content: `<div class="am-u-sm-12" style="padding:20px">
							<div class="am-u-sm-12 cl-p">
								<fieldset class="cl-p cl-m">
									<legend>用车权限详情</legend>
								</fieldset>
							</div>
							<div class="am-u-sm-12 cl-p">
								
								<div class="am-u-sm-12" style='padding-top:10px'>
									<div class='order_detail_h2'>权限信息</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">额度类型:</div>${limittype}
									</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">权限名称:</div>${detail_main.limit_name}
									</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">用车日期:</div>${use_date}
									</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">用车时间:</div>${use_time}
									</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">用车地点:</div>${address}
									</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">可用车型:</div>
										<div class='am-fl' style='width:200px'>
											<div>类型:${type}</div>
											<div>车辆类型:${car_type}</div>
										</div>
									</div>
									<div class="am-u-sm-12 cl-p order_detail_text">
										<div class="detail-list">每单限额:</div>${quota_day}
									</div>
									
									
								</div>
								<div class="am-u-sm-12" style='padding-top:10px'>
									${approve_flowpath}
								</div>
								
							</div>
						</div>`,
						success: function(res) {
						}
					})
				});
			}
		},
		error: function() {
			$("#loading-first").fadeOut(500);
		}
	});
}
//	<div class='order_detail_h2'>审批流程</div>
//									<div class="am-u-sm-12 cl-p order_detail_text">
//										<div class="detail-list">第一级审批人:</div>${one_approve}
//									</div>
//									<div class="am-u-sm-12 cl-p order_detail_text">
//										<div class="detail-list">第二级审批人:</div>${two_approve}
//									</div>
//									<div class="am-u-sm-12 cl-p order_detail_text">
//										<div class="detail-list">第三级审批人:</div>${three_approve}
//									</div>
	
	
})
